package p_stack_queue;

import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description: 最小栈
 * User: 王圆豪
 * Date: 2024-08-08
 * Time: 18:38
 */
public class MinStack {
    Stack<Integer> stack;
    Stack<Integer> minStack;

    public MinStack() {
        stack = new Stack<>();
        minStack = new Stack<>();
    }

    public void push(int val) {
        stack.push(val);
        if (minStack.isEmpty() || minStack.peek() >= val) {
            minStack.push(val);
        }
    }

    public void pop() {
        if (stack.pop().equals(minStack.peek())){
            minStack.pop();
        }
    }

    public int top() {
        return stack.peek();
    }

    public int getMin() {
        return minStack.peek();
    }

    public static void main(String[] args) {
        MinStack minStack = new MinStack();
        minStack.push(1);
        minStack.push(-1);
        minStack.push(12);
        minStack.push(5);
        minStack.push(4);
        System.out.println(minStack.getMin());
    }
}
